home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 September / PCWorld_2007-09_cd.bin / komunikace / firefox / Firefox Setup 2.0.0.6.exe / nonlocalized / components / nsBrowserContentHandler.js < prev    next >
Encoding:
Text File  |  2007-07-25  |  31.3 KB  |  896 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Mozilla Firefox browser.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Benjamin Smedberg <benjamin@smedbergs.us>
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsISupports            = Components.interfaces.nsISupports;
  39.  
  40. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  41. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  42. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  43. const nsIChannel             = Components.interfaces.nsIChannel;
  44. const nsICommandLine         = Components.interfaces.nsICommandLine;
  45. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  46. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  47. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  48. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  49. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  50. const nsIFactory             = Components.interfaces.nsIFactory;
  51. const nsIFileURL             = Components.interfaces.nsIFileURL;
  52. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  53. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  54. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  55. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  56. const nsISupportsString      = Components.interfaces.nsISupportsString;
  57. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  58. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  59. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  60. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  61. const nsICategoryManager     = Components.interfaces.nsICategoryManager;
  62. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  63. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  64. const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
  65.  
  66. const NS_BINDING_ABORTED = 0x804b0002;
  67. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  68. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  69.  
  70. function shouldLoadURI(aURI) {
  71.   if (aURI && !aURI.schemeIs("chrome"))
  72.     return true;
  73.  
  74.   dump("*** Preventing external load of chrome: URI into browser window\n");
  75.   dump("    Use -chrome <uri> instead\n");
  76.   return false;
  77. }
  78.  
  79. function resolveURIInternal(aCmdLine, aArgument) {
  80.   var uri = aCmdLine.resolveURI(aArgument);
  81.  
  82.   if (!(uri instanceof nsIFileURL)) {
  83.     return uri;
  84.   }
  85.  
  86.   try {
  87.     if (uri.file.exists())
  88.       return uri;
  89.   }
  90.   catch (e) {
  91.     Components.utils.reportError(e);
  92.   }
  93.  
  94.   // We have interpreted the argument as a relative file URI, but the file
  95.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  96.  
  97.   try {
  98.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  99.                              .getService(nsIURIFixup);
  100.  
  101.     uri = urifixup.createFixupURI(aArgument, 0);
  102.   }
  103.   catch (e) {
  104.     Components.utils.reportError(e);
  105.   }
  106.  
  107.   return uri;
  108. }
  109.  
  110. const OVERRIDE_NONE        = 0;
  111. const OVERRIDE_NEW_PROFILE = 1;
  112. const OVERRIDE_NEW_MSTONE  = 2;
  113. /**
  114.  * Determines whether a home page override is needed.
  115.  * Returns:
  116.  *  OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
  117.  *  OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
  118.  *                      Gecko milestone (i.e. right after an upgrade).
  119.  *  OVERRIDE_NONE otherwise.
  120.  */
  121. function needHomepageOverride(prefb) {
  122.   var savedmstone = null;
  123.   try {
  124.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  125.   } catch (e) {}
  126.  
  127.   if (savedmstone == "ignore")
  128.     return OVERRIDE_NONE;
  129.  
  130.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  131.                          .getService(nsIHttpProtocolHandler).misc;
  132.  
  133.   if (mstone != savedmstone) {
  134.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  135.     return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
  136.   }
  137.  
  138.   return OVERRIDE_NONE;
  139. }
  140.  
  141. // Copies a pref override file into the user's profile pref-override folder,
  142. // and then tells the pref service to reload it's default prefs.
  143. function copyPrefOverride() {
  144.   try {
  145.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  146.                                 .getService(Components.interfaces.nsIProperties);
  147.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  148.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  149.                                        Components.interfaces.nsIFile);
  150.     if (!prefOverride.exists())
  151.       return; // nothing to do
  152.  
  153.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  154.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  155.                                            Components.interfaces.nsIFile);
  156.  
  157.     // Check for any existing pref overrides, and remove them if present
  158.     var existingPrefOverridesFile = prefOverridesDir.clone();
  159.     existingPrefOverridesFile.append(prefOverride.leafName);
  160.     if (existingPrefOverridesFile.exists())
  161.       existingPrefOverridesFile.remove(false);
  162.  
  163.     prefOverride.copyTo(prefOverridesDir, null);
  164.  
  165.     // Now that we've installed the new-profile pref override file,
  166.     // re-read the default prefs.
  167.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  168.                                .getService(Components.interfaces.nsIObserver);
  169.     prefSvcObs.observe(null, "reload-default-prefs", null);
  170.   } catch (ex) {
  171.     Components.utils.reportError(ex);
  172.   }
  173. }
  174.  
  175. function openWindow(parent, url, target, features, args) {
  176.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  177.                          .getService(nsIWindowWatcher);
  178.  
  179.   var argstring;
  180.   if (args) {
  181.     argstring = Components.classes["@mozilla.org/supports-string;1"]
  182.                             .createInstance(nsISupportsString);
  183.     argstring.data = args;
  184.   }
  185.   return wwatch.openWindow(parent, url, target, features, argstring);
  186. }
  187.  
  188. function openPreferences() {
  189.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  190.   var url = "chrome://browser/content/preferences/preferences.xul";
  191.  
  192.   var win = getMostRecentWindow("Browser:Preferences");
  193.   if (win) {
  194.     win.focus();
  195.   } else {
  196.     openWindow(null, url, "_blank", features);
  197.   }
  198. }
  199.  
  200. function getMostRecentWindow(aType) {
  201.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  202.                      .getService(nsIWindowMediator);
  203.   return wm.getMostRecentWindow(aType);
  204. }
  205.  
  206. //@line 214 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  207.  
  208. // this returns the most recent non-popup browser window
  209. function getMostRecentBrowserWindow() {
  210.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  211.                      .getService(Components.interfaces.nsIWindowMediator);
  212.  
  213. //@line 234 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  214.   var windowList = wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
  215.   if (!windowList.hasMoreElements())
  216.     return null;
  217.  
  218.   var win = windowList.getNext();
  219.   while (!win.toolbar.visible) {
  220.     if (!windowList.hasMoreElements()) 
  221.       return null;
  222.  
  223.     win = windowList.getNext();
  224.   }
  225. //@line 246 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  226.  
  227.   return win;
  228. }
  229.  
  230. function doSearch(searchTerm, cmdLine) {
  231.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  232.                      .getService(nsIBrowserSearchService);
  233.  
  234.   var submission = ss.defaultEngine.getSubmission(searchTerm, null);
  235.  
  236.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  237.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  238.                      .createInstance(Components.interfaces.nsISupportsArray);
  239.  
  240.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  241.                        .createInstance(Components.interfaces.nsISupportsString);
  242.   wuri.data = submission.uri.spec;
  243.  
  244.   sa.AppendElement(wuri);
  245.   sa.AppendElement(null);
  246.   sa.AppendElement(null);
  247.   sa.AppendElement(submission.postData);
  248.  
  249.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  250.   // preferences, but need nsIBrowserDOMWindow extensions
  251.  
  252.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  253.                          .getService(nsIWindowWatcher);
  254.  
  255.   return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
  256.                            "_blank",
  257.                            "chrome,dialog=no,all" +
  258.                              nsBrowserContentHandler.getFeatures(cmdLine),
  259.                            sa);
  260. }
  261.  
  262. var nsBrowserContentHandler = {
  263.   /* helper functions */
  264.  
  265.   mChromeURL : null,
  266.  
  267.   get chromeURL() {
  268.     if (this.mChromeURL) {
  269.       return this.mChromeURL;
  270.     }
  271.  
  272.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  273.                           .getService(nsIPrefBranch);
  274.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  275.  
  276.     return this.mChromeURL;
  277.   },
  278.  
  279.   /* nsISupports */
  280.   QueryInterface : function bch_QI(iid) {
  281.     if (!iid.equals(nsISupports) &&
  282.         !iid.equals(nsICommandLineHandler) &&
  283.         !iid.equals(nsIBrowserHandler) &&
  284.         !iid.equals(nsIContentHandler) &&
  285.         !iid.equals(nsICommandLineValidator) &&
  286.         !iid.equals(nsIFactory))
  287.       throw Components.errors.NS_ERROR_NO_INTERFACE;
  288.  
  289.     return this;
  290.   },
  291.  
  292.   /* nsICommandLineHandler */
  293.   handle : function bch_handle(cmdLine) {
  294.     if (cmdLine.handleFlag("browser", false)) {
  295.       openWindow(null, this.chromeURL, "_blank",
  296.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  297.                  this.defaultArgs);
  298.       cmdLine.preventDefault = true;
  299.     }
  300.  
  301.     try {
  302.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  303.     }
  304.     catch (e) {
  305.       throw NS_ERROR_ABORT;
  306.     }
  307.  
  308.     if (remoteCommand != null) {
  309.       try {
  310.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  311.         var remoteVerb;
  312.         if (a) {
  313.           remoteVerb = a[1].toLowerCase();
  314.           var remoteParams = [];
  315.           var sepIndex = a[2].lastIndexOf(",");
  316.           if (sepIndex == -1)
  317.             remoteParams[0] = a[2];
  318.           else {
  319.             remoteParams[0] = a[2].substring(0, sepIndex);
  320.             remoteParams[1] = a[2].substring(sepIndex + 1);
  321.           }
  322.         }
  323.  
  324.         switch (remoteVerb) {
  325.         case "openurl":
  326.         case "openfile":
  327.           // openURL(<url>)
  328.           // openURL(<url>,new-window)
  329.           // openURL(<url>,new-tab)
  330.  
  331.           // First param is the URL, second param (if present) is the "target"
  332.           // (tab, window)
  333.           var url = remoteParams[0];
  334.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  335.           if (remoteParams[1]) {
  336.             var targetParam = remoteParams[1].toLowerCase()
  337.                                              .replace(/^\s*|\s*$/g, "");
  338.             if (targetParam == "new-tab")
  339.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  340.             else if (targetParam == "new-window")
  341.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  342.             else {
  343.               // The "target" param isn't one of our supported values, so
  344.               // assume it's part of a URL that contains commas.
  345.               url += "," + remoteParams[1];
  346.             }
  347.           }
  348.  
  349.           var uri = resolveURIInternal(cmdLine, url);
  350.           handURIToExistingBrowser(uri, target, cmdLine);
  351.           break;
  352.  
  353.         case "xfedocommand":
  354.           // xfeDoCommand(openBrowser)
  355.           if (remoteParams[0].toLowerCase() != "openbrowser")
  356.             throw NS_ERROR_ABORT;
  357.  
  358.           openWindow(null, this.chromeURL, "_blank",
  359.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  360.                      this.defaultArgs);
  361.           break;
  362.  
  363.         default:
  364.           // Somebody sent us a remote command we don't know how to process:
  365.           // just abort.
  366.           throw "Unknown remote command.";
  367.         }
  368.  
  369.         cmdLine.preventDefault = true;
  370.       }
  371.       catch (e) {
  372.         Components.utils.reportError(e);
  373.         // If we had a -remote flag but failed to process it, throw
  374.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  375.         // back to the handling code.
  376.         throw NS_ERROR_ABORT;
  377.       }
  378.     }
  379.  
  380.     var uriparam;
  381.     try {
  382.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  383.         var uri = resolveURIInternal(cmdLine, uriparam);
  384.         if (!shouldLoadURI(uri))
  385.           continue;
  386.         openWindow(null, this.chromeURL, "_blank",
  387.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  388.                    uri.spec);
  389.         cmdLine.preventDefault = true;
  390.       }
  391.     }
  392.     catch (e) {
  393.       Components.utils.reportError(e);
  394.     }
  395.  
  396.     try {
  397.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  398.         var uri = resolveURIInternal(cmdLine, uriparam);
  399.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  400.         cmdLine.preventDefault = true;
  401.       }
  402.     }
  403.     catch (e) {
  404.       Components.utils.reportError(e);
  405.     }
  406.  
  407.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  408.     if (chromeParam) {
  409.  
  410.       // Handle the old preference dialog URL separately (bug 285416)
  411.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  412.         openPreferences();
  413.       } else {
  414.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  415.         openWindow(null, chromeParam, "_blank", features, "");
  416.       }
  417.  
  418.       cmdLine.preventDefault = true;
  419.     }
  420.     if (cmdLine.handleFlag("preferences", false)) {
  421.       openPreferences();
  422.       cmdLine.preventDefault = true;
  423.     }
  424.     if (cmdLine.handleFlag("silent", false))
  425.       cmdLine.preventDefault = true;
  426.  
  427.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  428.     if (searchParam) {
  429.       doSearch(searchParam, cmdLine);
  430.       cmdLine.preventDefault = true;
  431.     }
  432.  
  433. //@line 454 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  434.     // Handle "? searchterm" for Windows Vista start menu integration
  435.     for (var i = cmdLine.length - 1; i >= 0; --i) {
  436.       var param = cmdLine.getArgument(i);
  437.       if (param.match(/^\? /)) {
  438.         cmdLine.removeArguments(i, i);
  439.         cmdLine.preventDefault = true;
  440.  
  441.         searchParam = param.substr(2);
  442.         doSearch(searchParam, cmdLine);
  443.       }
  444.     }
  445. //@line 466 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  446.   },
  447.  
  448.   helpInfo : "  -browser            Open a browser window.\n",
  449.  
  450.   /* nsIBrowserHandler */
  451.  
  452.   get defaultArgs() {
  453.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  454.                           .getService(nsIPrefBranch);
  455.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
  456.                               .getService(Components.interfaces.nsIURLFormatter);
  457.  
  458.     var overridePage = "";
  459.     var haveUpdateSession = false;
  460.     try {
  461.       switch (needHomepageOverride(prefb)) {
  462.         case OVERRIDE_NEW_PROFILE:
  463.           // New profile.
  464.           overridePage = formatter.formatURLPref("startup.homepage_welcome_url");
  465.           break;
  466.         case OVERRIDE_NEW_MSTONE:
  467.           // Existing profile.
  468.           copyPrefOverride();
  469.  
  470.           // Check whether we have a session to restore. If we do, we assume
  471.           // that this is an "update" session.
  472.           var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
  473.                              .getService(Components.interfaces.nsISessionStartup);
  474.           haveUpdateSession = ss.doRestore();
  475.           overridePage = formatter.formatURLPref("startup.homepage_override_url");
  476.           break;
  477.       }
  478.     } catch (e) {}
  479.  
  480.     // formatURLPref might return "about:blank" if getting the pref fails
  481.     if (overridePage == "about:blank")
  482.       overridePage = "";
  483.  
  484.     var startPage = "";
  485.     try {
  486.       var choice = prefb.getIntPref("browser.startup.page");
  487.       if (choice == 1)
  488.         startPage = this.startPage;
  489.  
  490.       if (choice == 2)
  491.         startPage = Components.classes["@mozilla.org/browser/global-history;2"]
  492.                               .getService(nsIBrowserHistory).lastPageVisited;
  493.     } catch (e) {
  494.       Components.utils.reportError(e);
  495.     }
  496.  
  497.     if (startPage == "about:blank")
  498.       startPage = "";
  499.  
  500.     // Only show the startPage if we're not restoring an update session.
  501.     if (overridePage && startPage && !haveUpdateSession)
  502.       return overridePage + "|" + startPage;
  503.  
  504.     return overridePage || startPage || "about:blank";
  505.   },
  506.  
  507.   get startPage() {
  508.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  509.                           .getService(nsIPrefBranch);
  510.  
  511.     var uri = prefb.getComplexValue("browser.startup.homepage",
  512.                                     nsIPrefLocalizedString).data;
  513.  
  514.     if (!uri) {
  515.       prefb.clearUserPref("browser.startup.homepage");
  516.       uri = prefb.getComplexValue("browser.startup.homepage",
  517.                                   nsIPrefLocalizedString).data;
  518.     }
  519.                                 
  520.     var count;
  521.     try {
  522.       count = prefb.getIntPref("browser.startup.homepage.count");
  523.     }
  524.     catch (e) {
  525.       return uri;
  526.     }
  527.  
  528.     for (var i = 1; i < count; ++i) {
  529.       try {
  530.         var page = prefb.getComplexValue("browser.startup.homepage." + i,
  531.                                          nsIPrefLocalizedString).data;
  532.         uri += "\n" + page;
  533.       }
  534.       catch (e) {
  535.       }
  536.     }
  537.  
  538.     return uri;
  539.   },
  540.  
  541.   mFeatures : null,
  542.  
  543.   getFeatures : function bch_features(cmdLine) {
  544.     if (this.mFeatures === null) {
  545.       this.mFeatures = "";
  546.  
  547.       try {
  548.         var width = cmdLine.handleFlagWithParam("width", false);
  549.         var height = cmdLine.handleFlagWithParam("height", false);
  550.  
  551.         if (width)
  552.           this.mFeatures += ",width=" + width;
  553.         if (height)
  554.           this.mFeatures += ",height=" + height;
  555.       }
  556.       catch (e) {
  557.       }
  558.     }
  559.  
  560.     return this.mFeatures;
  561.   },
  562.  
  563.   /* nsIContentHandler */
  564.  
  565.   handleContent : function bch_handleContent(contentType, context, request) {
  566.     try {
  567.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  568.                                  .getService(nsIWebNavigationInfo);
  569.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  570.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  571.       }
  572.     } catch (e) {
  573.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  574.     }
  575.  
  576.     var parentWin;
  577.     try {
  578.       parentWin = context.getInterface(nsIDOMWindow);
  579.     }
  580.     catch (e) {
  581.     }
  582.  
  583.     request.QueryInterface(nsIChannel);
  584.     
  585.     openWindow(parentWin, request.URI, "_blank", null, null);
  586.     request.cancel(NS_BINDING_ABORTED);
  587.   },
  588.  
  589.   /* nsICommandLineValidator */
  590.   validate : function bch_validate(cmdLine) {
  591.     // Other handlers may use osint so only handle the osint flag if the url
  592.     // flag is also present and the command line is valid.
  593.     var osintFlagIdx = cmdLine.findFlag("osint", false);
  594.     var urlFlagIdx = cmdLine.findFlag("url", false);
  595.     if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
  596.         cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
  597.       var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
  598.       if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
  599.         throw NS_ERROR_ABORT;
  600.       cmdLine.handleFlag("osint", false)
  601.     }
  602.   },
  603.  
  604.   /* nsIFactory */
  605.   createInstance: function bch_CI(outer, iid) {
  606.     if (outer != null)
  607.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  608.  
  609.     return this.QueryInterface(iid);
  610.   },
  611.     
  612.   lockFactory : function bch_lock(lock) {
  613.     /* no-op */
  614.   }
  615. };
  616.  
  617. const bch_contractID = "@mozilla.org/browser/clh;1";
  618. const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
  619. const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
  620.  
  621. function handURIToExistingBrowser(uri, location, cmdLine)
  622. {
  623.   if (!shouldLoadURI(uri))
  624.     return;
  625.  
  626.   var navWin = getMostRecentBrowserWindow();
  627.   if (!navWin) {
  628.     // if we couldn't load it in an existing window, open a new one
  629.     openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  630.                "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  631.                uri.spec);
  632.     return;
  633.   }
  634.  
  635.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  636.                      .getInterface(nsIWebNavigation);
  637.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  638.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  639.                         .getInterface(nsIDOMWindow);
  640.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  641.   bwin.openURI(uri, null, location,
  642.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  643. }
  644.  
  645.  
  646. var nsDefaultCommandLineHandler = {
  647.   /* nsISupports */
  648.   QueryInterface : function dch_QI(iid) {
  649.     if (!iid.equals(nsISupports) &&
  650.         !iid.equals(nsICommandLineHandler) &&
  651.         !iid.equals(nsIFactory))
  652.       throw Components.errors.NS_ERROR_NO_INTERFACE;
  653.  
  654.     return this;
  655.   },
  656.  
  657.   // List of uri's that were passed via the command line without the app
  658.   // running and have already been handled. This is compared against uri's
  659.   // opened using DDE on Win32 so we only open one of the requests.
  660.   _handledURIs: [ ],
  661. //@line 682 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  662.   _haveProfile: false,
  663. //@line 684 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  664.  
  665.   /* nsICommandLineHandler */
  666.   handle : function dch_handle(cmdLine) {
  667.     var urilist = [];
  668.  
  669. //@line 690 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  670.     // If we don't have a profile selected yet (e.g. the Profile Manager is
  671.     // displayed) we will crash if we open an url and then select a profile. To
  672.     // prevent this handle all url command line flags and set the command line's
  673.     // preventDefault to true to prevent the display of the ui. The initial
  674.     // command line will be retained when nsAppRunner calls LaunchChild though
  675.     // urls launched after the initial launch will be lost.
  676.     if (!this._haveProfile) {
  677.       try {
  678.         // This will throw when a profile has not been selected.
  679.         var fl = Components.classes["@mozilla.org/file/directory_service;1"]
  680.                            .getService(Components.interfaces.nsIProperties);
  681.         var dir = fl.get("ProfD", Components.interfaces.nsILocalFile);
  682.         this._haveProfile = true;
  683.       }
  684.       catch (e) {
  685.         while ((ar = cmdLine.handleFlagWithParam("url", false))) { }
  686.         cmdLine.preventDefault = true;
  687.       }
  688.     }
  689. //@line 710 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  690.  
  691.     try {
  692.       var ar;
  693.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  694.         var found = false;
  695.         var uri = resolveURIInternal(cmdLine, ar);
  696.         // count will never be greater than zero except on Win32.
  697.         var count = this._handledURIs.length;
  698.         for (var i = 0; i < count; ++i) {
  699.           if (this._handledURIs[i].spec == uri.spec) {
  700.             this._handledURIs.splice(i, 1);
  701.             found = true;
  702.             cmdLine.preventDefault = true;
  703.             break;
  704.           }
  705.         }
  706.         if (!found) {
  707.           urilist.push(uri);
  708.           // The requestpending command line flag is only used on Win32.
  709.           if (cmdLine.handleFlag("requestpending", false) &&
  710.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  711.             this._handledURIs.push(uri)
  712.         }
  713.       }
  714.     }
  715.     catch (e) {
  716.       Components.utils.reportError(e);
  717.     }
  718.  
  719.     count = cmdLine.length;
  720.  
  721.     for (i = 0; i < count; ++i) {
  722.       var curarg = cmdLine.getArgument(i);
  723.       if (curarg.match(/^-/)) {
  724.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  725.         // To emulate the pre-nsICommandLine behavior, we ignore
  726.         // the argument after an unrecognized flag.
  727.         ++i;
  728.       } else {
  729.         try {
  730.           urilist.push(resolveURIInternal(cmdLine, curarg));
  731.         }
  732.         catch (e) {
  733.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  734.         }
  735.       }
  736.     }
  737.  
  738.     if (urilist.length) {
  739.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  740.           urilist.length == 1) {
  741.         // Try to find an existing window and load our URI into the
  742.         // current tab, new tab, or new window as prefs determine.
  743.         try {
  744.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  745.           return;
  746.         }
  747.         catch (e) {
  748.         }
  749.       }
  750.  
  751.       var speclist = [];
  752.       for (uri in urilist) {
  753.         if (shouldLoadURI(urilist[uri]))
  754.           speclist.push(urilist[uri].spec);
  755.       }
  756.  
  757.       if (speclist.length) {
  758.         openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  759.                    "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  760.                    speclist.join("|"));
  761.       }
  762.  
  763.     }
  764.     else if (!cmdLine.preventDefault) {
  765.       openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  766.                  "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  767.                  nsBrowserContentHandler.defaultArgs);
  768.     }
  769.   },
  770.  
  771.   // XXX localize me... how?
  772.   helpInfo : "Usage: firefox [-flags] [<url>]\n",
  773.  
  774.   /* nsIFactory */
  775.   createInstance: function dch_CI(outer, iid) {
  776.     if (outer != null)
  777.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  778.  
  779.     return this.QueryInterface(iid);
  780.   },
  781.     
  782.   lockFactory : function dch_lock(lock) {
  783.     /* no-op */
  784.   }
  785. };
  786.  
  787. const dch_contractID = "@mozilla.org/browser/final-clh;1";
  788. const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
  789.  
  790. var Module = {
  791.   /* nsISupports */
  792.   QueryInterface: function mod_QI(iid) {
  793.     if (iid.equals(Components.interfaces.nsIModule) ||
  794.         iid.equals(Components.interfaces.nsISupports))
  795.       return this;
  796.  
  797.     throw Components.results.NS_ERROR_NO_INTERFACE;
  798.   },
  799.  
  800.   /* nsIModule */
  801.   getClassObject: function mod_getco(compMgr, cid, iid) {
  802.     if (cid.equals(bch_CID))
  803.       return nsBrowserContentHandler.QueryInterface(iid);
  804.  
  805.     if (cid.equals(dch_CID))
  806.       return nsDefaultCommandLineHandler.QueryInterface(iid);
  807.  
  808.     throw Components.results.NS_ERROR_NO_INTERFACE;
  809.   },
  810.     
  811.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  812.     var compReg =
  813.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  814.  
  815.     compReg.registerFactoryLocation( bch_CID,
  816.                                      "nsBrowserContentHandler",
  817.                                      bch_contractID,
  818.                                      fileSpec,
  819.                                      location,
  820.                                      type );
  821.     compReg.registerFactoryLocation( dch_CID,
  822.                                      "nsDefaultCommandLineHandler",
  823.                                      dch_contractID,
  824.                                      fileSpec,
  825.                                      location,
  826.                                      type );
  827.  
  828.     function registerType(contentType) {
  829.       compReg.registerFactoryLocation( bch_CID,
  830.                                        "Browser Cmdline Handler",
  831.                                        CONTRACTID_PREFIX + contentType,
  832.                                        fileSpec,
  833.                                        location,
  834.                                        type );
  835.     }
  836.  
  837.     registerType("text/html");
  838.     registerType("application/vnd.mozilla.xul+xml");
  839. //@line 860 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  840.     registerType("image/svg+xml");
  841. //@line 862 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8-release/WINNT_5.2_Depend/mozilla/browser/components/nsBrowserContentHandler.js"
  842.     registerType("text/rdf");
  843.     registerType("text/xml");
  844.     registerType("application/xhtml+xml");
  845.     registerType("text/css");
  846.     registerType("text/plain");
  847.     registerType("image/gif");
  848.     registerType("image/jpeg");
  849.     registerType("image/jpg");
  850.     registerType("image/png");
  851.     registerType("image/bmp");
  852.     registerType("image/x-icon");
  853.     registerType("image/vnd.microsoft.icon");
  854.     registerType("image/x-xbitmap");
  855.     registerType("application/http-index-format");
  856.  
  857.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  858.                            .getService(nsICategoryManager);
  859.  
  860.     catMan.addCategoryEntry("command-line-handler",
  861.                             "m-browser",
  862.                             bch_contractID, true, true);
  863.     catMan.addCategoryEntry("command-line-handler",
  864.                             "x-default",
  865.                             dch_contractID, true, true);
  866.     catMan.addCategoryEntry("command-line-validator",
  867.                             "b-browser",
  868.                             bch_contractID, true, true);
  869.   },
  870.     
  871.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  872.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  873.     compReg.unregisterFactoryLocation(bch_CID, location);
  874.     compReg.unregisterFactoryLocation(dch_CID, location);
  875.  
  876.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  877.                            .getService(nsICategoryManager);
  878.  
  879.     catMan.deleteCategoryEntry("command-line-handler",
  880.                                "m-browser", true);
  881.     catMan.deleteCategoryEntry("command-line-handler",
  882.                                "x-default", true);
  883.     catMan.deleteCategoryEntry("command-line-validator",
  884.                                "b-browser", true);
  885.   },
  886.  
  887.   canUnload: function(compMgr) {
  888.     return true;
  889.   }
  890. };
  891.  
  892. // NSGetModule: Return the nsIModule object.
  893. function NSGetModule(compMgr, fileSpec) {
  894.   return Module;
  895. }
  896.